package com.cncytech.uiautomator_demo;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.cncytech.uiautomator_demo.util.Threada;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private static final int PERMISSON_REQUESTCODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void checkPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        }

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        checkPermission();
    }

    public void Onc(View view) {
      /*  WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        wifiManager.startScan();

        for (ScanResult scanResult : wifiManager.getScanResults()) {
            Log.i(TAG, "Onc: " + scanResult.SSID);
            Toast.makeText(this, scanResult.SSID, Toast.LENGTH_SHORT).show();
        }
*/
        execCmd("am instrument -w com.cncytech.uiautomator_demo.test/androidx.test.runner.AndroidJUnitRunner");

    }
    public void execCmd(String cmd) {
        Log.i(TAG, "execCmd: ---execCmd:  " + cmd);
        Process p = null;
        try {
            p = Runtime.getRuntime().exec(cmd);
            // 启用StreamGobbler线程清理错误流和输入流 防止IO阻塞
            new Threada(p.getErrorStream(), "ERROR").start();
            new Threada(p.getInputStream(), "INPUT").start();
            p.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (p != null && p.exitValue() != 0) {
                p.destroy();
            }
        }
    }

}